﻿using System;
using System.Text;

namespace Runtime.Models {

    /// <summary>
    /// 账号数据
    /// </summary>
    public interface IDFR_AccountModel : IDFR_Model {

        private static readonly string MappingTable = "ABCFGNOIJYZDPQTMUV0RS23E6KL154HWX789,";

        IDFR_AccountModelSetter AsSetter();

        public static string EncodeInviterCode(string input) {
            StringBuilder result = new StringBuilder();
            foreach (char c in input) {
                int index = (int)c % MappingTable.Length; // 使用字符的 ASCII 码映射
                result.Append(MappingTable[index]);
            }
            return result.ToString();
        }

        public static string DecodeInviterCode(string encoded) {
            var result = new StringBuilder();
            foreach (char c in encoded) {
                int index = MappingTable.IndexOf(c);
                if (index == -1)
                    throw new ArgumentException("Invalid encoded string");
                result.Append((char)index);
            }
            return result.ToString();
        }

        IReadOnlyBoolData IsLogined { get; }

        /// <summary>
        /// 用户名
        /// </summary>
        string Username { get; }

        /// <summary>
        /// 用户ID
        /// </summary>
        long UID { get; }

        /// <summary>
        /// 服务器ID
        /// </summary>
        int ServerID { get; }

        /// <summary>
        /// 平台
        /// </summary>
        string Platform { get; }

        /// <summary>
        /// 平台ID
        /// </summary>
        string PlatformID { get; }

        /// <summary>
        /// 邀请码
        /// </summary>
        string InviterCode { get; }
    }

    [Implement(typeof(IDFR_AccountModel))]
    internal sealed class DFR_AccountModel : DFR_Model, IDFR_AccountModel, IDFR_AccountModelSetter {

        public IDFR_AccountModelSetter AsSetter() => this;

        [Autowired]
        public IReadOnlyBoolData IsLogined { get; private set; }

        public string Username { get; private set; }

        public long UID { get; private set; }

        public int ServerID { get; private set; }

        public string Platform { get; private set; }

        public string PlatformID { get; private set; }

        public string InviterCode { get; private set; }

        void IDFR_AccountModelSetter.SetUsername(string username) => Username = username;

        void IDFR_AccountModelSetter.SetUID(long value) => UID = value;

        void IDFR_AccountModelSetter.SetPlatform(string value) => Platform = value;

        void IDFR_AccountModelSetter.SetPlatformID(string value) => PlatformID = value;

        void IDFR_AccountModelSetter.SetServerID(int value) => ServerID = value;

        void IDFR_AccountModelSetter.SetInviterCode(string value) => InviterCode = value;

        void IDFR_AccountModelSetter.Clear() {
            UID = 0;
            ServerID = 0;
            Platform = null;
            PlatformID = null;
            InviterCode = null;
        }
    }

    public interface IDFR_AccountModelSetter {

        void SetUsername(string value);

        void SetUID(long value);

        void SetServerID(int value);

        void SetPlatform(string value);

        void SetPlatformID(string value);

        void SetInviterCode(string value);

        void Clear();
    }
}
